package com.i2bt.app.files.model;

import com.i2bt.common.AndroidRestContract;
import com.i2bt.common.UserConfig;

import com.i2bt.app.BaseActivity;
import com.i2bt.app.BaseJsonModel;
import com.i2bt.app.files.FilesSharedActivity;
import com.i2bt.bean.FileInfoBean;
import com.i2bt.bean.FriendInfoBean;
import com.i2bt.bean.GetSharedBean;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

public class FilesSharedJsonHandle extends BaseJsonModel {

	public FilesSharedJsonHandle(BaseActivity activity) {
		super(activity);
	}

	@Override
	protected String logicToWrap() throws JSONException {
		
		switch (beanParam.getAct()) {

		// 获取好友列表
		case AndroidRestContract.SHOW_FRIEND : {
			jsonStrParam = wrapShowFriend();
			break;
		}
		
		// 分享大量文件
		case AndroidRestContract.ADD_MULT_SHARE : {
			jsonStrParam = wrapAddShared();
			break;
		}
		
		}

		return jsonStrParam;
	}
	
	/**
	 * 打包获取好友列表的json
	 * @return
	 * @throws JSONException
	 */
	private String wrapShowFriend() throws JSONException {
		
		wrapCommonJson();
		
		return jsonParam.toString();
	}
	
	/**
	 * 打包分享大量文件给大量好友
	 * @return
	 * @throws JSONException
	 */
	private String wrapAddShared() throws JSONException {
		
		wrapCommonJson();
		
		GetSharedBean sharedBean = (GetSharedBean) beanParam;
		
		try {
			jsonParam.put(AndroidRestContract.SHARE_DATE, 
					sharedBean.getShareDate())
					.put(AndroidRestContract.PARENT_PATH, 
							URLEncoder.encode(UserConfig.currentFilesPath, "utf-8"))
					.put(AndroidRestContract.SHARE_FOLDERS, 
							wrapSharedFiles(sharedBean.getSharePathList()))
					.put(AndroidRestContract.SHARE_FRIENDS, 
							wrapSharedFriend(sharedBean.getShareList()));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		
		return jsonParam.toString();
		
	}
	
	/**
	 * 打包分享大量文件为json数组
	 * @return
	 * @throws JSONException 
	 * @throws UnsupportedEncodingException 
	 */
	private JSONArray wrapSharedFiles(FileInfoBean[] sharedPathList) throws JSONException, UnsupportedEncodingException {
		JSONArray sharedPathsJsonArr = new JSONArray();
		for (int i = 0; i < sharedPathList.length; i++) {
			FileInfoBean fileInfoBean = sharedPathList[i];
			JSONObject sharedPathJsonObj = new JSONObject();
			String fileName = URLEncoder.encode(fileInfoBean.getFileName(), "utf-8");
			String fileLength = URLEncoder.encode(fileInfoBean.getFileLength() + "", "utf-8");
			sharedPathJsonObj.put(AndroidRestContract.FILES_SHARE_PATH, fileName)
					.put(AndroidRestContract.FILE_SIZE, fileLength);
			sharedPathsJsonArr.put(sharedPathJsonObj);
		}
		return sharedPathsJsonArr;
	}
	
	/**
	 * 打包分享给大量的用户为json数组
	 * @param sharedList
	 * @return
	 */
	private JSONArray wrapSharedFriend(String[] sharedList) {
		JSONArray sharedFriendJsonArr = new JSONArray();
		for (int i = 0; i < sharedList.length; i++) {
			sharedFriendJsonArr.put(sharedList[i]);
		}
		return sharedFriendJsonArr;
	}
	

	@Override
	protected void logicToUnwrap(boolean isSuc, String result)
			throws JSONException {
		
		if (isSuc) {
			jsonParam = new JSONObject(result);

			if (jsonParam.getBoolean(AndroidRestContract.SUCCEED)) {
				FilesSharedActivity friendSharedActivity = (FilesSharedActivity) activity;

				switch  (jsonParam.getInt(AndroidRestContract.ACTION)) {

				//显示好友列表的响应处理
				case AndroidRestContract.SHOW_FRIEND : {
					friendSharedActivity.showFriendResponse(unwrapShowFriend());
					break;
				}

				}
			}
			else {
				activity.toastHandler(jsonParam.
						getString(AndroidRestContract.MESSAGE));
			}
		}
		else {
			activity.toastHandler(result);
		}
	}
	
	/**
	 * 解包显示好友的json
	 * @return
	 * @throws JSONException
	 */
	private List<FriendInfoBean> unwrapShowFriend() throws JSONException {
		
		List<FriendInfoBean> friendInfoList = new ArrayList<FriendInfoBean>();
		JSONArray jsonArrayTemp = jsonParam.getJSONArray(
				AndroidRestContract.FRIEND_LIST);
		for (int i = 0; i < jsonArrayTemp.length(); i++) {
			friendInfoList.add( wrapFriendBean(jsonArrayTemp.getJSONArray(i)) );
		}
		
		return friendInfoList;
	}
	
	/**
	 * 把解包的好友放入到好友信息类里面
	 * @param jsonArray
	 * @return
	 * @throws JSONException
	 */
	private FriendInfoBean wrapFriendBean(JSONArray jsonArray) throws JSONException {
		FriendInfoBean infoBean = new FriendInfoBean();
		infoBean.setFriendId(jsonArray.getString(0));
		infoBean.setFriendName(jsonArray.getString(1));
		return infoBean;
	}
}
